﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class TeamNameChange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            if (User.Identity.IsAuthenticated != false)
            {
                if (!IsPostBack)
                {
                    if (ou.TeamId == null)
                        Response.Redirect("GeneralMessage.aspx?MessageString=l4OF1EOTHJA");

                    // put team name into the text box
                    TeamNameTextBox.Text = GND.ReadNivdalStringData("TeamId", "Nivdal_GetTeamNameByTeamId", ou.TeamId);

                    // update change cost:
                    decimal Cost = Decimal.Parse(GND.ReadNivdalIntData("Nivdal_GetTeamNameChangeCost"));
                    ChangeCostLabel.Text = Cost.ToString("N0");
                    SubmitButton.OnClientClick = "return confirm('" + Resources.Resource.ChangeTeamName_ApproveTextPart1 + " " +
                                                  Cost.ToString("N0") + " " + Resources.Resource.ChangeTeamName_ApproveTextPart2 + "')";

                    this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ChangeTeamName_PageTitle;
                    //Title = Resources.Resource.ChangeTeamName_PageTitle + " - Nivdal.com!";
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        protected void CheckAvailability(object sender, EventArgs e)
        {
            if (IsTaken(TeamNameTextBox.Text.Trim()))
            {
                AvailabilityLabel.Text = Resources.Resource.ChangeTeamName_NameNotAvailable;
            }
            else
            {
                AvailabilityLabel.Text = Resources.Resource.ChangeTeamName_NameAvailable;
                SubmitButton.Visible = true;
                CheckAvailabilityButton.Visible = false;
                RenewButton.Visible = true;
                TeamNameTextBox.Enabled = false;
            }
        }

        protected void SubmitNewName(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = GND.ReadNivdalStringData("@UserId", "@TeamName", "Nivdal_ChangeTeamName", ou.UserId, TeamNameTextBox.Text.Trim());
            Response.Redirect("TeamHomePage.aspx?Message=" + Answer);
        }

        protected void Renew(object sender, EventArgs e)
        {
            AvailabilityLabel.Text = "";
            SubmitButton.Visible = false;
            CheckAvailabilityButton.Visible = true;
            RenewButton.Visible = false;
            TeamNameTextBox.Enabled = true;
        }

        private bool IsTaken(string TeamName)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Nivdal_GetSimilarTeamNames", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("TeamName", System.Data.SqlDbType.NVarChar, 256);
            cmd.Parameters["TeamName"].Value = TeamName;
            SqlDataReader Reader;
            bool ReturnValue = false;

            try
            {
                conn.Open();
                Reader = cmd.ExecuteReader();
                if (Reader.HasRows)
                    while (Reader.Read())
                    {
                        if (Reader.GetString(0) == TeamName)
                        {
                            ReturnValue = true;
                            break;
                        }
                    }
                Reader.Close();
            }
            finally
            {
                conn.Close();
            }
            return ReturnValue;
        }
        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}